/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.odichy.andfml.ui;

import org.odichy.andfml.R;
import org.odichy.andfml.content.AccountFactory;
import org.odichy.andfml.util.Constant;
import org.odichy.andfml.util.Md5;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

/**
 * AndFML, an FMyLife.com client on Google Android platform.
 *
 * @author Odin Liu<odinushuaia@gmail.com>
 *
 * @version 1.0.0
 */
public class MainActivity extends Activity {
	public static final String ACCOUNT_SETTING = "account_setting";
	public static final String USERNAME = "login";
	public static final String PASSWORD = "password";
	public static final String REMEM = "remem";
	public static final String TOKEN = "token";
	
	private Intent intent;
	private AccountFactory factory;
	private SharedPreferences setting;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setting = getSharedPreferences(ACCOUNT_SETTING, 0);
        
        Button viewButton = (Button)findViewById(R.id.justview);
        viewButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//intent = new Intent(MainActivity.this, LastViewActivity.class);
				intent = new Intent(MainActivity.this, TabViewActivity.class);
				startActivity(intent);
			}
        });
        Button loginButton = (Button)findViewById(R.id.login);
        loginButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
				login();
			}
        });
        Button signButton = (Button)findViewById(R.id.signup);
        signButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
				// TODO com
        		Toast.makeText(MainActivity.this, R.string.sorry_for_key, Toast.LENGTH_SHORT).show();
        		//signup();
			}
        });
        
        if(setting.getBoolean(REMEM, false)) {
        	factory = new AccountFactory(Constant.ACCOUNT_BASE_URL, setting.getString(USERNAME, null), setting.getString(PASSWORD, null), Constant.KEY, Constant.LANGUAGE_EN);
        	if(factory.getAccount()!=null&&factory.getAccount().getCode()==1) {
        		setting.edit().putString(TOKEN, factory.getAccount().getToken()).commit();
        		intent = new Intent(MainActivity.this, TabViewActivity.class);
        		startActivity(intent);
        	}
        	else {
        		Toast.makeText(MainActivity.this, R.string.connection_error, Toast.LENGTH_SHORT).show();
        	}
        }
        
        EditText username = (EditText)findViewById(R.id.input_username);
        username.setText(setting.getString(USERNAME, ""));
        EditText password = (EditText)findViewById(R.id.input_password);
        password.setText(setting.getString(PASSWORD, ""));
        CheckBox cb = (CheckBox)findViewById(R.id.if_remember);
        cb.setChecked(setting.getBoolean(REMEM, false));
    }
    
    @Override
    protected void onDestroy() {
    	factory = new AccountFactory(Constant.ACCOUNT_BASE_URL, setting.getString(TOKEN, ""), Constant.KEY, Constant.LANGUAGE_EN);
    	super.onDestroy();
    }
    
    private void login() {
    	EditText editText = (EditText)findViewById(R.id.input_username);
    	CharSequence username = editText.getText();
    	editText = (EditText)findViewById(R.id.input_password);
    	CharSequence pass = editText.getText();
    	setting.getString(PASSWORD, null);
    	String password = setting.getString(PASSWORD, null)==null?new Md5(pass.toString()).toString():setting.getString(PASSWORD, null);
    	CheckBox cb = (CheckBox)findViewById(R.id.if_remember);
    	factory = new AccountFactory(Constant.ACCOUNT_BASE_URL, username.toString(), password, Constant.KEY, Constant.LANGUAGE_EN);
    	if(cb.isChecked()) {
    		setting.edit().putString(USERNAME, username.toString()).putString(PASSWORD, password).putBoolean(REMEM, true).commit();
    	}
    	else {
    		setting.edit().putString(USERNAME, null).putString(PASSWORD, null).putBoolean(REMEM, false).commit();
    	}
    	if(factory.getAccount()!=null&&factory.getAccount().getCode()==1) {
    		setting.edit().putString(TOKEN, factory.getAccount().getToken()).commit();
    		intent = new Intent(MainActivity.this, TabViewActivity.class);
    		startActivity(intent);
    	}
    	else {
    		Toast.makeText(MainActivity.this, R.string.login_error, Toast.LENGTH_SHORT).show();
    	}
    }
    
    private void signup() {
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.signup, (ViewGroup) findViewById(R.id.sign_root));
    	builder.setView(layout).setPositiveButton(R.string.sign_submit, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
    	}).setNegativeButton(R.string.sign_cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
    	});
    	
    	AlertDialog alertDialog = builder.create();
    	alertDialog.show();
    	/*
    	dialog.setContentView(R.layout.signup);
    	dialog.setTitle(R.string.sign_title);
    	dialog.setCancelable(true);
    	dialog.set*/
    }
}